package com.soft.collateral.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.soft.collateral.constant.MessageConstant;
import com.soft.collateral.entity.StorageWithdrawRecord;
import com.soft.collateral.result.Result;
import com.soft.collateral.service.RecordService;
import com.soft.collateral.vo.PageQueryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/record")
@Api(tags = "后台系统-出入库记录接口")
@RequiredArgsConstructor
public class RecordController {

    private final RecordService recordService;

    @ApiOperation("查询出入库记录列表")
    @GetMapping("list")
    public Result list() {
        List<StorageWithdrawRecord> recordList = recordService.listAll();
        if (!recordList.isEmpty()) {
            return new Result(true, MessageConstant.QUERY_RECORD_SUCCESS, recordList);
        }
        return new Result(false, MessageConstant.QUERY_RECORD_FAIL);
    }

    @ApiOperation("条件分页查询出入库记录列表")
    @GetMapping("page")
    public Result page(PageQueryVO pageQueryVO) {
        Page<StorageWithdrawRecord> pageQuery = recordService.pageQuery(pageQueryVO);
        if (pageQuery != null) {
            return new Result(true, MessageConstant.QUERY_RECORD_SUCCESS, pageQuery);
        }
        return new Result(false, MessageConstant.QUERY_RECORD_FAIL);
    }

    @ApiOperation("通过ID查询出入库记录信息")
    @GetMapping("{id}")
    public Result findById(@PathVariable Integer id) {
        StorageWithdrawRecord record = recordService.getByIdWithDetails(id);
        if (record != null) {
            return new Result(true, MessageConstant.QUERY_RECORD_SUCCESS, record);
        }
        return new Result(true, MessageConstant.QUERY_RECORD_SUCCESS);
    }
}